const fse = require('fs-extra');
const { logger } = require('./logger');

function renameSync(src, dest) {
    if (!fse.existsSync(src)) return;

    fse.renameSync(src, dest);
}

function moveSync(src, dest) {
    if (!fse.existsSync(src)) return;

    logger.log(`move ${src} to ${dest}`);
    fse.moveSync(src, dest, { overwrite: true });
    logger.log(`move ${src} to ${dest} done`);
}

function removeSync(src) {
    if (!fse.existsSync(src)) return;

    logger.log(`remove ${src}`);
    fse.removeSync(src);
    logger.log(`remove ${src} done`);
}

removeSync("../build/res");

moveSync("../build/wechatgame/res", "../build/res");
moveSync("../build/wechatgame/res_internal", "../build/wechatgame/res");

fse.ensureDirSync("../build/wechatgame/engine");
moveSync("../build/wechatgame/adapter-min.js", "../build/wechatgame/engine/adapter-min.js");
moveSync("../build/wechatgame/adapter.js", "../build/wechatgame/engine/adapter.js");
moveSync("../build/wechatgame/ccRequire.js", "../build/wechatgame/engine/ccRequire.js");

moveSync("../build/wechatgame/game.js", "../build/wechatgame/engine/game.js");
moveSync("../build/wechatgame/main.js", "../build/wechatgame/engine/main.js");
moveSync("../build/wechatgame/src", "../build/wechatgame/engine/src");

renameSync("../build/wechatgame/GameBackup.js", "../build/wechatgame/game.js");
let content = fse.readFileSync("../build/wechatgame/engine/game.js", "utf8");
content = content.replace("require('cocos/cocos2d-js-min.js');", "require('../cocos/cocos2d-js-min.js');");
fse.writeFileSync("../build/wechatgame/engine/game.js", content, "utf-8");